package ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.peng.auctionsystem.R;

import java.util.List;

import entity.CommondityListEntity;
import manager.ApiConstants;

/**
 * 竞品适配器
 */

public class HomePageLotsAdapter extends BaseAdapter {
    private Context mContext;
    private List<CommondityListEntity.DataBean.RowsBean> mCommodityListData;

    public HomePageLotsAdapter(Context context, List<CommondityListEntity.DataBean.RowsBean> commodityListData) {
        this.mContext = context;
        mCommodityListData = commodityListData;
    }

    public void updateAdapter(List<CommondityListEntity.DataBean.RowsBean> list) {
        mCommodityListData.clear();
        mCommodityListData.addAll(list == null ? mCommodityListData : list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mCommodityListData.size();
    }

    @Override
    public Object getItem(int position) {
        return mCommodityListData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.layout_lot_item, null);
            holder.mIvImg = convertView.findViewById(R.id.lot_item_img);
            holder.mTvTitle = convertView.findViewById(R.id.lot_item_tv_title);
            holder.mTvDesc = convertView.findViewById(R.id.lot_item_tv_desc);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Glide.with(mContext).load(ApiConstants.STAGING_MOBILE_TICKET_HOST + "commodity/" + mCommodityListData.get(position).getImagePath()).into(holder.mIvImg);
        holder.mTvTitle.setText(mCommodityListData.get(position).getTitle());
        holder.mTvDesc.setText(mCommodityListData.get(position).getRemark());
        return convertView;
    }

    public class ViewHolder {
        ImageView mIvImg;
        TextView mTvTitle;
        TextView mTvDesc;
    }
}
